package model.drawServer.Implement;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Map;
import model.drawPanel.*;
import model.drawPanel.entitys.*;
import model.drawServer.Interface.IDrawServer;
import model.roomManagement.Implementation.Server;

public class DrawServer implements IDrawServer{
	private ArrayList<DrawRoom>roomList;
	private Thread udpThread;
	private Thread tcpThread;
	public static int drawServerUdpPort = 10810;//绘图信息专用端口号
	public static int drawClientUdpPort = 10820;
	public static int drawServerTcpPort = 10830;
	public static int drawClientTcpPort = 10840;
	public static int drawServerInitePort = 10850;
	public static int drawClientInitePort = 10860;
	public DrawServer (){
		roomList = new ArrayList<DrawRoom>();
	}
	@Override
	public void start() {
		 udpThread = new Thread(new UdpThread(),"udpThread");
		 tcpThread = new Thread(new TcpThread(),"tcpThread");
		udpThread.start();
		tcpThread.start();
	}
	public void stop(){
		udpThread =null;
		tcpThread = null;
	}
	private Object byte_to_object (byte[] data){
		ByteArrayInputStream byteBuffer = new ByteArrayInputStream(data);
		try {
			ObjectInputStream buffers;
			buffers = new ObjectInputStream(byteBuffer);
			Object result = buffers.readObject();
			buffers.close();
			return result;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			
		}
		return null;
		
	}
	class UdpThread implements Runnable{
		DatagramSocket udp;
		public UdpThread(){
			try {
				this.udp = new DatagramSocket(drawServerUdpPort);
			} catch (SocketException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		@Override
		public void run() {
			while(true){
				byte[] data = new byte[1024];
				DatagramPacket packet = new DatagramPacket(data,data.length);
				try {
					udp.receive(packet);
					Order order =(Order) byte_to_object(data);
					for(DrawRoom room :roomList){
						if(room.getRoomID() == order.getRoomID()){
							room.receiveMsg(order,packet.getAddress());
							break;
						}
					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
		}
	}
	class TcpThread implements Runnable{
		ServerSocket s;
		/*public DrawRoom read(){
			try {
				ObjectInputStream in = new ObjectInputStream(new FileInputStream(new File("a.txt")));
				try {
					return (DrawRoom)in.readObject();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}*/
		@Override
		public void run() {
			
			try {
				s= new ServerSocket(drawServerTcpPort);
				while(true){
					Socket client = s.accept();
					ClientProxy cp = new ClientProxy(client);
					int roomID = cp.getRoomID();
					boolean isNewRoom = true;
					for(DrawRoom room :roomList){
						if(room.getRoomID() ==roomID){
							room.userEnter(cp);
							isNewRoom = false;
							break;
						}
					}
					if(isNewRoom){
						/*DrawRoom room =(DrawRoom) Server.getDB().getRoom(roomID);
						if(room ==null){*/
						DrawRoom room = new DrawRoom(roomID);
					/*	}else{
							room.setDs(DrawServer.this);
						}*/
						room.userEnter(cp);
						roomList.add(room);
					}
					
				}
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}  
			// TODO Auto-generated method stub
			
		}
		
	}
	/*public static void main(String[] args){
		DrawServer server = new DrawServer();
		server.start();
		while(true){
			try {
				Thread.currentThread().sleep(1000000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	 */
	public void roomLeave(int roomID){
		DrawRoom del = null;
		for(DrawRoom room : roomList){
			if(room.getRoomID() == roomID){
				del =room;
			}
		}
		if(del !=null){
			roomList.remove(del);
		}
	}

}
